/*
 * @Author: chestnut_647 
 * @Date: 2017-07-21 13:44:29 
 * @Last Modified by: chestnut_647
 * @Last Modified time: 2017-08-02 20:06:15
 */
require('./index.css');
require('page/common/nav/index.js');
require('page/common/header/index.js');
var navSide  = require('page/common/nav-side/index.js');
var _mm = require('util/mm.js');
var _order = require('service/order-service.js');
var templateIndex = require('./index.string');

var page = {
  data: {
    orderNumber: _mm.getUrlParam('orderNumber')
  },
  init: function() {
    this.onLoad();
    this.bindEvent();
  },
  onLoad: function() {
    // 初始化左侧菜单
    navSide.init({name: 'order-list'});
    // 加载用户信息
    this.loadDetail();
  },
  loadDetail: function() {
    var _this = this,
      orderDetailHtml = '',
      $content = $('.content');
    $content.html('<div class="loading"></div>');
    _order.getOrderDetail(_this.data.orderNumber, function(res) {
      _this.dataFilter(res);
      orderDetailHtml = _mm.renderHtml(templateIndex, res);
      $content.html(orderDetailHtml);
    }, function(errMsg) {
      $content.html('<p class="err-tip">'+ errMsg +'</p>');
    })
  },
  dataFilter: function(data) {
    data.needPay = (data.status == 10);
    data.isCancelable = (data.status == 10);
  },
  bindEvent: function() {
    var _this = this;
    $(document).on('click', '.order-cancel', function() {
      if(window.confirm('确定要删除订单')) {
         _order.cancelOrder(_this.data.orderNumber, function(res) {
            _mm.successTip('该订单取消成功');
            _this.loadDetail();
          }, function(errMsg) {
            _mm.errorTips(errMsg);
          })
      }
    });
  }
};
$(function() {
  page.init();
  
})